import 'dart:io';
import 'package:currency_balance/http/request_interceptors.dart';
import 'package:currency_balance/http/result_data.dart';
import 'package:dio/dio.dart';
import '../generated/l10n.dart';
import 'code.dart';

typedef OnLocalDataCallback = void Function(String jsonStr);

class HttpManager{


  static final HttpManager _instance = HttpManager._internal();
  late Dio _dio;
  late String baseUrl = 'https://api.linkseed.io';
  HttpManager._internal(){
    _dio = Dio(
        BaseOptions(
          connectTimeout: Duration(seconds: 10),
          baseUrl: baseUrl,
        ),
      );
    _dio.interceptors.add(RequestInterceptors());
  }

  static HttpManager getInstance({String? baseUrl}) {
    if (null == baseUrl) {
      return _instance._normal();
    } else {
      return _instance._baseUrl(baseUrl);
    }
  }

  HttpManager _baseUrl(String? baseUrl) {
    if (baseUrl != null) {
      _dio.options.baseUrl = baseUrl;
    }
    return this;
  }

  HttpManager _normal() {
    if (_dio.options.baseUrl != baseUrl) {
      _dio.options.baseUrl = baseUrl;
    }
    return this;
  }

  ///通用的GET请求
  Future<ResultData> get(api, Map<String,dynamic>? params, {headers, noTip = false}) async {

    Response response;
    try {
      if (headers != null) {
        _dio.options.headers.addAll(headers);
      }
      response = await _dio.get(api, queryParameters: params);
    } on DioException catch (e) {
      return resultError(e);
    }

    if (response.data is DioException) {
      return resultError(response.data['code']);
    }

    return response.data;
  }

  ///通用的POST请求
  Future<ResultData> post(api, params, {data,noTip = false, cancelToken}) async {
    Response response;
    try {
      response = await _dio.post(api,data: data, queryParameters: params, cancelToken: cancelToken);
    } on DioError catch (e) {
      print('post error:$e');
      return resultError(e);
    }
    return response.data;
  }

}

ResultData resultError(DioException e) {
  ResultData resultData;
  // if (e.type == DioExceptionType.connectionTimeout) {
  //   resultData = ResultData(
  //       false, Code.NETWORK_CONNECT_TIMEOUT, S.current.network_connect_timeout);
  // } else if (e.type == DioExceptionType.sendTimeout) {
  //   resultData = ResultData(
  //       false, Code.NETWORK_REQUEST_TIMEOUT, S.current.network_request_timeout);
  // } else if (e.type == DioExceptionType.receiveTimeout) {
  //   resultData = ResultData(false, Code.NETWORK_RESPONSE_TIMEOUT,
  //       S.current.network_response_timeout);
  // } else if (e.type == DioExceptionType.badResponse) {
  //   int statusCode = e.response?.statusCode??-1;
  //   print('error=========:${e.toString()}');
  //   if (statusCode == 400) {
  //     resultData = ResultData(
  //         false, statusCode, S.current.remote_server_or_network_is_abnormal);
  //   } else if (statusCode == 401) {
  //     resultData =
  //         ResultData(false, statusCode, S.current.login_status_expired);
  //   } else if (statusCode == 404) {
  //     resultData = ResultData(
  //         false, statusCode, S.current.remote_server_or_network_is_abnormal);
  //   } else if (statusCode == 502) {
  //     resultData = ResultData(false, statusCode, S.current.server_upgrade);
  //   } else if (statusCode == 503) {
  //     resultData = ResultData(false, statusCode, S.current.server_is_busy);
  //   } else {
  //     resultData = ResultData(false, statusCode, S.current.unknown_error);
  //   }
  // } else if (e.type == DioExceptionType.cancel) {
  //   resultData =
  //       ResultData(false, Code.NETWORK_CANCEL, S.current.cancel_request);
  // } else {
  //   String errorMsg = S.current.unknown_error;
  //   if(e.error is SocketException){
  //     var exception = e.error as SocketException;
  //     if(exception.osError?.errorCode == 8){
  //       errorMsg = S.current.network_error;
  //     }
  //   }
    resultData =
        ResultData(false, Code.UNKNOWN_EXCEPTION, 'unknown error');
  // }

  return resultData;
}